﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    /// <summary>
    /// 单日账户交易合约(那些小计的合约信息也直接在这里面写入)当Account为-1时为总数
    /// </summary>
    public class AccountMonthTradeInstrument
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 交易合约
        /// </summary>
        public int InstrumentId { get; set; }
        /// <summary>
        /// 交易合约
        /// </summary>
        public virtual Instrument Instrument { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }




        ///// <summary>
        ///// 删除当月所有记录
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.AccountMonthTradeInstrument where t.Month == month && t.Year == year select t;
        //    foreach (Data.Month.AccountMonthTradeInstrument c in query)
        //    {
        //        db.AccountMonthTradeInstrument.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 保存账号月变化情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitMonthAccountInstrumentChange(int month, int year, DB.DBManager db)
        //{
        //    var query = db.Accounts.ToList();
        //    foreach (Sys.Account account in query)
        //    {
        //        #region i为0是多，i为1是空
        //        for (int i = 0; i < 2; i++)
        //        {
        //            var _query = (from t in db.AccountDayTradeInstrument where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i select t).ToList();
        //            foreach (Day.AccountDayTradeInstrument instru in _query)
        //            {
        //                var query2 = (from t in db.AccountMonthTradeInstrument where t.Month == instru.TradeHistory.MPdate && t.Year == instru.TradeHistory.YPdate && t.AccountId == account.Id && t.Direction == i && t.InstrumentId == instru.InstrumentId select t).ToList();
        //                var query3 = (from t in db.AccountDayTradeInstrument where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i && t.InstrumentId == instru.InstrumentId select t).ToList();
        //                AccountMonthTradeInstrument change = new AccountMonthTradeInstrument();
        //                change.AccountId = account.Id;
        //                change.Direction = i;
        //                change.InstrumentId = instru.InstrumentId;
        //                if (query2.Count() > 0)//有记录更新
        //                {
        //                    change = query2.First();
        //                }
        //                if (_query.Count() > 0)
        //                {
        //                    change.BillCount = query3.Sum(a => a.BillCount);
        //                    change.DayCount = query3.Sum(a => a.DayCount);
        //                    change.Free = query3.Sum(a => a.Free);
        //                    change.LoseCount = query3.Sum(a => a.LoseCount);
        //                    change.Profit = query3.Sum(a => a.Profit);
        //                    change.SumLost = query3.Sum(a => a.SumLost);
        //                    change.SumWin = query3.Sum(a => a.SumWin);
        //                    change.UsedMargin = query3.Sum(a => a.UsedMargin);
        //                    change.WinCount = query3.Sum(a => a.WinCount);
        //                }
        //                change.Year = year;
        //                change.Month = month;
        //                if (query2.Count() == 0)
        //                {
        //                    db.AccountMonthTradeInstrument.Add(change);
        //                }
        //            }

        //        }
        //        #endregion
        //    }
        //    db.SaveChanges();
        //}
    }
}